<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=0">
  <meta http-equiv="X-UA-Compatible" content="ie=edge">
  <title>阳江之旅抽奖（2017.08.12）</title>
  <style>
    #go {
      position: absolute;
      left: 0;
      height: 44px;
      bottom: 0;
      width: 50%;
      background: red;
      color: #fff;
      text-align: center;
      line-height: 44px;
    }
    #reset {
      position: absolute;
      right: 0;
      height: 44px;
      bottom: 0;
      width: 50%;
      background: blue;
      color: #fff;
      text-align: center;
      line-height: 44px;
    }
    #all {
      margin-bottom: 64px;
    }
    #all li {
      line-height: 32px;
    }
    #input {
      position: absolute;
      top: 10px;
      right: 10px;
      width: 50px;
      height: 50px;
      background: red;
      color: #fff;
      line-height: 50px;
      border-radius: 100%;
      text-align: center;
      font-size: 22px;
    }
    .del {
      margin-left: 20px;
      color: red;
    }
    .shake {
      animation:shake 1s 3;
      -webkit-animation:shake 100ms 10;
      transform-origin: center center;
    }
    @keyframes shake
    {
      0% {
        transform: skewY(-20deg);
      }
      50% {
        transform: skewY(20deg);
      }
      100% {
        transform: skewY(-20deg);
      }
    }

    @-webkit-keyframes shake /*Safari and Chrome*/
    {
      0% {
        -webkit-transform: skewY(-20deg);
      }
      50% {
        -webkit-transform: skewY(20deg);
      }
      100% {
        -webkit-transform: skewY(-20deg);
      }
    }
  </style>
</head>
<body>
  <h1>中奖名单：<i id="wintotal">(0)</i></h1>
  <ol id="win"></ol>
  <h1>参与名单：<i id="total">(0)</i></h1>
  <ol id="all"></ol>
  <div id="go">开始抽奖</div>
  <div id="reset">重新抽奖</div>
  <div id="input">加</div>
  <script>
    // 密码
    var password = localStorage.getItem('foreveross_password')
    
    var win = document.getElementById('win')
    var all = document.getElementById('all')

    function ajax (path, callback) {
      var xhr = new XMLHttpRequest()
      xhr.onreadystatechange = function () {
        if (xhr.readyState === XMLHttpRequest.DONE) {
          callback(xhr.responseText)
        }
      }
      xhr.open('GET', path, true)
      xhr.send()
    }

    function confirmPassword(pw) {
      if (!pw) {
        pw = prompt('请输入密码')
      }
      if (Number(pw) >>1<<2>>3<<4 === 2656) {
        password = pw
        localStorage.setItem('foreveross_password', pw)
        return true
      } else {
        alert('密码错误')
        return false
      }
    }

    // 抽奖
    document.getElementById('go').onclick = function () {
      if (!confirmPassword(password)) {
        return
      }
      ajax('/data.json', function (data) {
        data = JSON.parse(data || [])
        var num = Math.floor(Math.random() * data.length)
        // 抽奖次数
        var count = 1000
        ajax('/win.json', function (b) {
          b = JSON.parse(b || [])
          if (b.length < Math.min(count, data.length)) {
            while (b.indexOf(data[num]) !== -1) {
              num = Math.floor(Math.random() * data.length)
            }
            ajax('/win?name=' + data[num], function (result) {
              if (result === 'success') {
                document.body.classList.add('shake')
                setTimeout(function () {
                  alert(data[num] + '中奖啦！！！')
                  location.reload()
                }, 1000)
              } else {
                alert('发生错误，灰长报欠！！')
              }
            })
          } else {
            alert('抽完了，还抽。。')
          }
        })
      })
    }

    // 删除
    all.addEventListener('click', function (e) {
      if (e.target.className === 'del') {
        var name = e.target.dataset.name
          if (!confirmPassword(password)) {
          return
        }
        ajax('/del?name=' + name, function (result) {
          if (result === 'success') {
            location.reload()
          } else {
            alert('发生错误，灰长报欠！！')
          }
        })
      }
    }, false)

    // 录入
    document.getElementById('input').onclick = function () {
      var name = prompt('输入姓名')
      if (name) {
        if (!/[\u4e00-\u9fa5]{2,3}/.test(name)) {
          alert('咳咳，认真点！！')
          return
        }
        ajax('/data.json', function (data) {
          data = JSON.parse(data || [])
          if (data.indexOf(name) === -1) {
            data.push(name)
            ajax('/add?name=' + name, function (result) {
              if (result === 'success') {
                location.reload()
              } else {
                alert('发生错误，灰长报欠！！')
              }
            })
          } else {
            alert('魂淡！想作弊？没门')
          }
        })
      }
    }

    // 重置
    document.getElementById('reset').onclick = function () {
      if (!confirmPassword(password)) {
        return
      }
      if (!confirm('确定要重新抽奖吗？')) {
        return
      }
      ajax('/reset', function (result) {
        if (result === 'success') {
          location.reload()
        } else {
          alert('发生错误，灰长报欠！！')
        }
      })
    }

    ajax('/win.json', function (data) {
      data = JSON.parse(data || [])
      var html = ''
      for (var i = 0; i < data.length; i++) {
        html += '<li>' + data[i] + '</li>'
      }
      document.getElementById('wintotal').innerText = '(' + data.length + ')'
      win.innerHTML = html
    })

    ajax('/data.json', function (data) {
      data = JSON.parse(data || [])
      var html = ''
      for (var i = 0; i < data.length; i++) {
        html += '<li>' + data[i] + '<span class="del" data-name="' + data[i] + '">删除</span></li>'
      }
      document.getElementById('total').innerText = '(' + data.length + ')'
      all.innerHTML = html
    })
  </script>
</body>
</html>